﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataInput;
using Slaks.Graphs.GraphBuilder;
using Slaks.Graphs.Algorithms;

namespace UI
{
    public partial class ExperimentDesign : Form
    {
        List<Group> TaskFieldsGroup = new List<Group>();
        List<Group> GraphGroup = new List<Group>();

        public void initializeFields()
        {
            Dictionary<string, object> task = new Dictionary<string, object>();
            List<Field> innerFields = new List<Field> ();            
            task.Add("Categorization", TaskType.Categorization);
            task.Add("Clustering", TaskType.Clustering);
            innerFields.Add(new ListField("Task Type:", TaskType.Categorization, task));
            innerFields.Add(new NumericField("Iterations:", "30"));
            innerFields.Add(new TextField("Input directory:"));
            innerFields.Add(new TextField("Output directory:"));
            innerFields.Add(new BooleanField("Skip parsing:", "1"));
            innerFields.Add(new BooleanField("Remove Stop Words:", "1"));
            innerFields.Add(new TextField("Output file name:"));
            Group TaskFields = new Group("Task properties:", innerFields, true);
            innerFields = new List<Field>();
            Dictionary<string, object> builderTypes = GraphBuilderBase.getAllBuilderTypes();
            innerFields.Add(new ListField("Builder Type:", BuilderType.StandardSimpleRepresentationSlaks, builderTypes));            
            Group GraphFields = new Group("Graph Builder:", innerFields, true);
            TaskFieldsGroup.Add(TaskFields);
            innerFields = new List<Field>();
            innerFields.Add(new NumericField("Graph Size:", "5"));
            innerFields.Add(new NumericField("n Parameter:", "0"));
            Group GraphParameters = new Group("Graph Parameters:", innerFields, true);
            Dictionary<string, object> distanceAlgorithms = new Dictionary<string,object> ();           
            distanceAlgorithms.Add("distance1", "distance1");
            distanceAlgorithms.Add("distance2", "distance2");
            innerFields = new List<Field>();
            innerFields.Add(new ListField("distance1", distanceAlgorithms));
            Group Distances = new Group("Distance Algorithms:", innerFields, true);
            GraphGroup.Add(GraphFields);
            GraphGroup.Add(GraphParameters);
            //GraphGroup.Add(Distances);
        }

        void replaceGroup( List<Group> groups, Group group, int position )
        {
            int i = 0;
            for (i = 0; (i < groups.Count && groups[i].GroupName != group.GroupName); i++) ;
            if (i < groups.Count)
            {
                groups.Remove(groups[i]);
            }
            if (position > groups.Count)
                position = groups.Count;       
            groups.Insert(position, group);
        }

        void removeGroup(List<Group> groups, Group group)
        {
            int i = 0;
            for (i = 0; (i < groups.Count && groups[i].GroupName != group.GroupName); i++) ;
            if (i < groups.Count)
            {
                groups.Remove(groups[i]);
            }            
        }


        void inputControl2_fieldChanged(object sender, EventArgs e)
        {
            Field field = (Field)sender;
            if (field.FieldName == "Builder Type:")
            {               
                BuilderType BT = ((BuilderType)field.Value);
                string[] DA = DistanceAlgFactory.Instance.GetSuitableDistanceAlgorithms(BT);
                Dictionary<string, object> distanceAlgorithms = new Dictionary<string, object>();
                for (int i = 0; i < DA.Length; i++)
                    distanceAlgorithms.Add(DA[i], DA[i]);
                List<Field> innerFields = new List<Field>();
                innerFields = new List<Field>();
                innerFields.Add(new ListField("Distance Algorithms", distanceAlgorithms));
                Group Distances = new Group("Distance Algorithms:", innerFields, true);
                if (distanceAlgorithms.Count > 0)
                    replaceGroup(GraphGroup, Distances, 2);
                else
                    removeGroup(GraphGroup, Distances);
            }
            inputControl2.setFields(GraphGroup);
        }

        public ExperimentDesign()
        {
            InitializeComponent();
            initializeFields();
            inputControl1.setFields(TaskFieldsGroup);
            inputControl2.setFields(GraphGroup);
            inputControl2.fieldChanged += new EventHandler(inputControl2_fieldChanged);
        }

        private void button1_Click(object sender, EventArgs e)
        {            
            this.DialogResult = DialogResult.OK;
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            Close();
        }
    }

    public enum TaskType
    {
        Categorization,
        Clustering
    }
}
